<?php

namespace app\yj\controller;

use app\yj\model\RoleModel;
use app\yj\model\RuleModel;
use app\yj\model\UserModel;
use Couchbase\Role;
use think\Controller;
use think\Paginator;
use think\Request;
use think\route\Rule;

class RoleController extends BaseAdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $roles = RoleModel::select()->toArray();
        foreach ($roles as $k => $v) {
            if (count($v['rules']) > 0) {
                $roles[$k]['rulesarr'] = [];
                foreach ($v['rules'] as $ru) {
                    $rule = RuleModel::with('menu')->find($ru);
                    if ($rule) array_push($roles[$k]['rulesarr'], $rule);

                }
            }
        }
        return $this->SuccessReturn('获取成功', $roles);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $data = $request->post();
        //获取参数
        $name = array_key_exists('name', $data) ? $data['name'] : '';
        $rules = array_key_exists('rules', $data) ? is_array($data['rules']) ? $data['rules'] : [] : [];
        if ($name == '') return $this->ErrorReturn(403, '缺少必要参数');
        //检查是否存在相同名称
        $name_role = RoleModel::where('name', $name)->find();
        if ($name_role) return $this->ErrorReturn(400, '存在相同的角色名称');
        $role = new RoleModel();
        $role->name = $name;
        //如果权限规则不为空
        if (count($rules) > 0) {
            foreach ($rules as $v) {
                $rule = RuleModel::find($v);
                if (!$rule) return $this->ErrorReturn(400, '不存在对应的权限');
            }
        }
        $role->rules = $rules;
        $res = $role->save();
        if ($res) return $this->SuccessReturn('添加成功', $role);
        return $this->ErrorReturn('添加失败');
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->put();
        //获取参数
        $id = array_key_exists('id', $data) ? $data['id'] : '';
        $name = array_key_exists('name', $data) ? $data['name'] : '';
        $rules = array_key_exists('rules', $data) ? is_array($data['rules']) ? $data['rules'] : [] : "";

        if ($id == '') return $this->ErrorReturn(403, '缺少id参数');

        //检查是否存在role
        $role = RoleModel::find($id);
        if (!$role) return $this->ErrorReturn(400, '不存在角色');
        if ($name != '') {
            $namedata = RoleModel::where('name', $name)->find();
            if ($namedata && $namedata->id != $id) {
                return $this->ErrorReturn(400, '存在相同的角色名称');
            } else {
                $role->name = $name;
            }
        }
        if ($rules != '') $role->rules = $rules;
        $res = $role->save();
        if ($res) return $this->SuccessReturn('修改成功', $role);
        return $this->ErrorReturn(400, '修改失败');

    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id, Request $request)
    {
        //
        if ($id == 0) return $this->ErrorReturn(400, '角色id无效');
        //查询是否存在此用户
        $role = RoleModel::where('id', $id)->find();
        if (!$role) return $this->ErrorReturn(404, '找不到此角色');

        //判断下角色下是否存在用户
        $user = UserModel::where('roles', $role->id)->find();
        if ($user) return $this->ErrorReturn(400, '角色下存在用户 不能删除');
        $role->delete();
        return $this->SuccessReturn('删除成功');
    }
}
